<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of account
 *
 * @author b4by
 */
class account extends MY_Controller {

	public function index() {
//		pr($_SERVER);
	}

	public function fb_login() {

		$ref = request('ref');

		if ($ref == '')
			$ref = url_create('site');

		if (preg_match('/' . addcslashes(url_create('site', 'account', 'fb_login'), '/.\\*+[])(|?^') . '/i', $ref) != 0)
			$ref = url_create('site');

		if ($this->isUserLogin())
			redirect($ref);

		$this->load->library('facebook');
		$this->load->model('model_user');

		$code = request('code');

		if (strlen($code) == 0) {
			redirect($this->facebook->getLoginUrl(array(
						'scope' => 'email,user_about_me'
					)));
		}
		$graph_url = "https://graph.facebook.com/me?access_token="
				. $this->facebook->getAccessToken();

		$user = json_decode(file_get_contents($graph_url));
		
		$user->username = isset($user->username)?$user->username:$user->id;
		
//		pr($user);die;
		
		if ($user->id == 0) {
			echo 'Not authentication';
			exit;
		}
		$info = $this->model_user->get_info_rule(array('fb_id' => $user->id));

		if ($info !== false) {
			$this->model_user->update($info->id, array(
				'username' => $user->username,
				'fullname' => $user->name,
				'email'		=> $user->email,
			));
			$this->session->set_userdata('user_id', $info->id);
			redirect($ref);
		}
//		echo 'die';die;
		$data = array(
			'email' => $user->email,
			'username' => $user->username,
			'fb_id' => $user->id,
			'active' => 1,
			'group_id' => $this->siteConfig->default_user_group,
			'fullname' => $user->name,
			'join_date' => now()
				);
//		pr($data);
		$user_id = $this->model_user->create($data);
//		echo 'die';die;

		$this->session->set_userdata('user_id', $user_id);

		redirect($ref);
	}

	public function fb_logout() {
		$this->session->set_userdata('user_id', $this->siteConfig->guest_id);
		set_cookie('username', '');
		set_cookie('password', '');
		redirect(url_create('site'));
	}

	public function images($userId = '', $page = 1) {
		if (!$userId)
			$userId = $this->user->id;
//		echo $userId;
		$this->load->model('model_image');

		$pagination = pagination_init($this->model_image->get_total(array('user_id' => $userId)), $page);

		$images = $this->model_image->get_list(array(
			'where' => array('user_id' => $userId),
			'limit' => array($pagination->from, $pagination->limit)
				));

		$uploader = $this->model_user->get_info($userId);
		if (!$uploader)
			redirect(url_create('site', 'account', 'images'));
		$this->data['site_title'] = $uploader->fullname . ' - ' . $this->data['site_title'];
		$this->data['uploader'] = $uploader;
		$this->data['images'] = $images;
		$this->data['temp'] = 'account/images';
		$this->data['pagination'] = $pagination;
		$this->loadView();
	}

	public function profile() {
		$this->data['profile'] = $this->user;
		$this->data['temp'] = 'account/profile';
		$this->loadView();
	}

}

?>
